/**
 * 
 */
package ar.com.gtug.repository.impl;

import java.io.Serializable;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jdo.support.JdoDaoSupport;

import ar.com.gtug.service.Repository;

/**
 * @author Juan M. Irungaray (juan@irungaray.com.ar)
 * 
 */
public abstract class BaseRepository<T> extends JdoDaoSupport implements
		Repository<T> {


	@Autowired
	public void setFactory(PersistenceManagerFactory persistenceManagerFactory) {
		setPersistenceManagerFactory(persistenceManagerFactory);
	}

	public void delete(T object) {
		getJdoTemplate().deletePersistent(object);
	}

	public void delete(Long id) {
		PersistenceManager pm = getPersistenceManager();
		pm.currentTransaction().begin();

		T objectTodelete = pm.getObjectById(getManagedEntity(), id);

		pm.deletePersistent(objectTodelete);

		pm.currentTransaction().commit();
	}

	public void delete(List<T> objects) {
		getJdoTemplate().deletePersistentAll(objects);
	}

	public List<T> getAll() {
		return getWithCriteriaAndOrder(null, null);
	}

	protected List<T> getWithCriteria(String filters) {
		return getWithCriteriaAndOrder(filters, null);
	}

	protected List<T> getWithOrder(String ordering) {
		return getWithCriteriaAndOrder(null, ordering);
	}

	@SuppressWarnings("unchecked")
	protected List<T> getWithCriteriaAndOrder(String filters, String ordering) {
		PersistenceManager pm = getPersistenceManager();
		pm.currentTransaction().begin();

		Query query = getQuery(pm, filters, ordering, "");

		List<T> result = (List<T>) query.execute();
		pm.currentTransaction().commit();
		return result;
	}

	@SuppressWarnings("unchecked")
	protected List<T> getTopWithCriteriaAndOrder(String filters,
			String ordering, String top) {
		PersistenceManager pm = getPersistenceManager();
		pm.currentTransaction().begin();

		Query query = getQuery(pm, filters, ordering, top);

		List<T> result = (List<T>) query.execute();
		pm.currentTransaction().commit();
		return result;
	}

	private Query getQuery(PersistenceManager pm, String filters,
			String ordering, String top) {
		Query query = pm.newQuery(getManagedEntity());

		if (filters != null) {
			query.setFilter(filters);
		}
		if (ordering != null) {
			query.setOrdering(ordering);
		}

		if (top != null && !"".equals(top)) {
			query.setRange("0," + top);
		}

		return query;
	}

	public T get(Serializable id) {
		T result = (T) getJdoTemplate().getObjectById(getManagedEntity(), id);
		System.out.println(JDOHelper.getPersistenceManager(result));
		return result;
	}


	public void save(T object) {
		System.out.println(JDOHelper.getPersistenceManager(object));
		getJdoTemplate().makePersistent(object);
	}

	public void save(List<T> objects) {
		getJdoTemplate().makePersistentAll(objects);

	}
	
	protected abstract Class<T> getManagedEntity();

}
